<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic" ],
            function(array, xhr, request, topic) {
                var deletItemsCount = 0;
                carrepairRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    document.getElementById("carrepairdate").value = "";
                    document.getElementById("carrepairdateu").value = "";
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#carrepairGird").jqGrid('getGridParam', 'selrow');
                        var carrepair = jQuery("#carrepairGird").jqGrid('getRowData', rowid);
                        dijit.byId('carrepairUFD').setValues(carrepair);
                    }
                };
                topic.subscribe("carrepair/insertOrUpdate", carrepairRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#carrepairGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#carrepairGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#carrepairGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#carrepairGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                carrepairSubmitForm = function(formId, type) {
                    var url = "carrepair/insert";
                    if (type == 'u') {
                        url = "carrepair/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = dojo.toJson(form.getValues(), true);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("carrepair/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                carrepairDelete = function() {
                    var ids = jQuery("#carrepairGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var carrepair = {};
                            carrepair.id = '';
                            array.forEach(ids, function(entry, i) {
                                carrepair.id += entry + ",";
                            });
                            syncDB("carrepair/delete", dojo.toJson(carrepair, true));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                carrepairQuery = function(formId) {
                    var carrepair = dijit.byId(formId).getValues();
                    jQuery("#carrepairGird").jqGrid('setGridParam', {
                        postData : carrepair
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="carrepairIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="carid">车牌号： </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="carid"></td>
      </tr>
      <tr>
        <td><label for="repairdate">维护日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="repairdate" onClick="WdatePicker()" id="carrepairdate"></td>
      </tr>
      <tr>
        <td><label for="reason">维护原因: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="reason"></td>
      </tr>
      <tr>
        <td><label for="executant">经办人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text" name="executant"></td>
      </tr>
      <tr>
        <td><label for="repairtype">维修类型： </label></td>
        <td><select data-dojo-type="dijit/form/Select" name="repairtype" data-dojo-props="required: true, trim:true" style="width: 182px;">
            <option value="1">保养</option>
            <option value="2">维修</option>
        </select></td>
      </tr>
      <tr>
        <td><label for="fee">费用: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="trim:true,regExp: '^(-?\\d+)(\\.\\d+)?$', invalidMessage:'请输入数字'" type="text" name="fee"></td>
      </tr>
      <tr>
        <td><label for="notes">备注: </label></td>
        <td><input data-dojo-type="dijit/form/SimpleTextarea" data-dojo-props="trim:true" type="text" name="notes"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carrepairSubmitForm('carrepairIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carrepairRestForm('carrepairIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="carrepairUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text" name="repairid" style="display: none;">
    <table>
      <tr>
        <td><label for="carid">车牌号: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="carid"></td>
      </tr>
      <tr>
        <td><label for="repairdate">维护日期: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="repairdate" class="Wdate" onClick="WdatePicker()" id="carrepairdateu"></td>
      </tr>
      <tr>
        <td><label for="reason">维护原因: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="reason"></td>
      </tr>
      <tr>
        <td><label for="executant">经办人: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="executant"></td>
      </tr>
      <tr>
        <td><label for="repairtype">维修类型： </label></td>
        <td><select data-dojo-type="dijit/form/Select" name="repairtype" data-dojo-props="required: true" style="width: 182px;">
            <option value="1">保养</option>
            <option value="2">维修</option>
        </select></td>
      </tr>
      <tr>
        <td><label for="fee">费用: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="trim:true,regExp: '^(-?\\d+)(\\.\\d+)?$', invalidMessage:'请输入数字'" type="text" name="fee"></td>
      </tr>
      <tr>
        <td><label for="notes">备注: </label></td>
        <td><input data-dojo-type="dijit/form/SimpleTextarea" type="text" name="notes"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carrepairSubmitForm('carrepairUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="carrepairRestForm('carrepairUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryCarRepairForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table cellspacing="10">
        <tr>
          <td><label for="carid">车牌号: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" type="text" name="carid"></td>
          <td><label for="repairtype">维修类型： </label></td>
          <td><select data-dojo-type="dijit/form/Select" name="repairtype" style="width: 100px;">
              <option value="1">保养</option>
              <option value="2">维修</option>
          </select></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="carrepairInsertBtn" data-dojo-type="dijit/form/Button" type="button" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('carrepairIFD').show();
      </script>
    </button>
    <button id="carrepairDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="carrepairDelete();"></button>
    <button id="carrepairUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#carrepairGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var carrepair = jQuery("#carrepairGird").jqGrid('getRowData', ids[0]);
          dijit.byId('carrepairUFD').setValues(carrepair);
          dijit.byId('carrepairUFD').show();
        }
      </script>
    </button>
    <button id="carrepairQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询" onclick="carrepairQuery('queryCarRepairForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="carrepairGird"></table>
  <div id="carrepairToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#carrepairGird").jqGrid({
        url : 'carrepair/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [ '维护ID', '车牌号', '维护日期', '维护原因', '经办人', '备注', '维修类型', '维护类型' ,'费用' ],
        colModel : [ {
            name : 'repairid',
            index : 'repairid',
            width : 60,
            key : true,
            hidden : true,
            align : 'center'
        }, {
            width : 120,
            name : 'carid',
            index : 'carid'
        }, {
            name : 'repairdate',
            index : 'repairdate',
            width : 120,
            align : 'center'
        }, {
            name : 'reason',
            index : 'reason',
            width : 250,
            align : 'center'
        }, {
            name : 'executant',
            index : 'executant',
            width : 100,
            align : 'center'
        }, {
            name : 'notes',
            index : 'notes',
            hidden : true
        }, {
            name : 'repairtype',
            index : 'repairtype',
            width : 100,
            align : 'center',
            hidden : true
        }, {
            name : 'repairtypeview',
            index : 'repairtypeview',
            width : 100,
            align : 'center'
        }, {
            name : 'fee',
            index : 'fee',
            width : 80,
            align : 'center'
        } ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#carrepairToolbar',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        gridComplete : function() {
            var ids = jQuery("#carrepairGird").jqGrid('getDataIDs');
            for ( var i = 0; i < ids.length; i++) {
                var cl = ids[i];
                var carcheck = jQuery("#carrepairGird").jqGrid('getRowData', cl);
                var status = carcheck['repairtype'] == '1' ? '保养' : (carcheck['repairtype'] == '2' ? '维修' : '维修');
                jQuery("#carrepairGird").jqGrid('setRowData', cl, {
                    repairtypeview : status
                });
            }
        },
        
        multiselect : true,
        shrinkToFit : false,
        caption : "车辆维护表列表"
    });
    jQuery("#carrepairGird").jqGrid('navGrid', '#carrepairToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
